﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmTimPhieuNK : Form
    {
        // the delegate the subscribers must implement
        public delegate void ChonPhieuNhapKho(object frmTimPhieuYC_KetQua, ChonPhieuNKEventArgs chonPNKevent);

        // an instance of the delegate
        public ChonPhieuNhapKho chonPNK;

        List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;

        public frmTimPhieuNK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            this.InitDMKho(_matau);
        }


        private void InitDMKho(string matau)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lstKho = new List<KHO>();
            lstKho = khoCtrl.LoadKHOByTAU(matau);
            this.lookupKho.Properties.DataSource = lstKho;
            this.lookupKho.Properties.DisplayMember = "MaKho";
            this.lookupKho.Properties.ValueMember = "MaKho";
        }


        private void cmdTim_Click(object sender, EventArgs e)
        {
            int daduyet = -1;
            PHIEU_NHAP_HANG objPNH = new PHIEU_NHAP_HANG();

            objPNH.MaKho = this.lookupKho.Text;
            objPNH.MaPhieuNhapHang = this.txtSoPhieuGR.Text.Trim();
            objPNH.POThamChieuSAP = this.txtSoPhieuPO.Text.Trim();
            objPNH.NguoiNhap = this.txtNguoiThucHien.Text.Trim();
            objPNH.NgayNhapFrom = this.dateEditFromDate.DateTime;
            objPNH.NgayNhapTo = this.dateEditToDate.DateTime;

            if (radioButton1.Checked == true)
            {
                daduyet = 1;
            }
            if (radioButton2.Checked == true)
            {
                daduyet = 0;
            }
            if (radioButton4.Checked == true)
            {
                objPNH.TinhTrang = 1;
            }

            List<PHIEU_NHAP_HANG> lst = new List<PHIEU_NHAP_HANG>();
            clsPHIEU_NHAP_HANG pnhCtrl = new clsPHIEU_NHAP_HANG(_sConn);
            lst = pnhCtrl.usp_SelectPHIEU_NHAP_HANGsSearch(objPNH, lstVTPT,daduyet);
            if (lst.Count > 0)
            {
                if (lst.Count >= 1)
                {
                    this.Hide();
                    frmTimPhieuNK_KetQua f = new frmTimPhieuNK_KetQua(_sConn, lst);
                    f.chonPNK += new frmTimPhieuNK_KetQua.ChonPhieuNhapKho(GetPhieuNhapKho);
                    f.Location = new Point(
                        (this.Location.X + this.Width / 2 + 100) - (f.Width / 2),
                        (this.Location.Y + this.Height / 2 + 100) - (f.Height / 2));
                    f.StartPosition = FormStartPosition.Manual;  

                    f.ShowDialog();
                    this.CloseTimPhieuNK();
                }
              
            }
            else
                MessageBox.Show("KHÔNG TÌM THẤY PHIẾU NHẬP KHO THỎA ĐIỀU KIỆN");

        }

        private void frmTimPhieuNK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                this.cmdTim_Click(null, null);
            }
        }

        private void lnkTimVTPT_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.lstVTPT.Clear();
            // goi form Search VTPT

        }
        public void CloseTimPhieuNK()
        {
            this.Dispose();
        }

        private void GetPhieuNhapKho(object sender, ChonPhieuNKEventArgs e)
        {            

            ChonPhieuNKEventArgs pnkevent = new ChonPhieuNKEventArgs(e.MaPhieuNhapKho);

            if (chonPNK != null)
            {
                chonPNK(this, pnkevent);
            }
        }

        private void frmTimPhieuNK_Paint(object sender, PaintEventArgs e)
        {
            Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new frmTimKiemVTPT(_sConn);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
            
        }
        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                this.lblTenVTPT.Text = lstVTPT[0]._tenVTPT;
            }
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm1 == null)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
           
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
        }
    }
}